package com.cgy.bionimec.controller;

import com.cgy.bionimec.entity.Binghuan;

import com.cgy.bionimec.service.BinghuanService;
import com.cgy.bionimec.util.LayuiJsonUilt;
import com.cgy.bionimec.util.ResultInfoUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@CrossOrigin
@RequestMapping("/binghuan")
public class BinghuanController {
    @Autowired
    private BinghuanService binghuanService;

    @GetMapping("/getAll")
    public LayuiJsonUilt<Binghuan> findAllUsers(LayuiJsonUilt<Binghuan> LJ,Binghuan b, Integer limit, Integer page) {
        Pageable pageable = PageRequest.of(LJ.getPage(),LJ.getLimit());

        Page<Binghuan> us = binghuanService.findAllBinghuan(b, pageable);
        LJ.setData(us.getContent());
        LJ.setCount(us.getTotalElements());
        return LJ;
    }

    @PostMapping("/addBinghuan")
    public Binghuan addBinghuan(Binghuan b){
        Date date = new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = format.format(date);
        b.setCreattime(format1);
        return binghuanService.insertOrUpdate(b);

    }
    /**
     * 修改

     * @return
     */
    @PutMapping("/updateBinghuan")
    public Binghuan updateUsers(Binghuan b,Integer id) {
    Binghuan b1 = binghuanService.findBinghuanById(id);
    b.setCount(b1.getCount());
    b.setCreattime(b1.getCreattime());

        return binghuanService.insertOrUpdate(b);
    }
    @DeleteMapping("/delete")
    public Integer deleteBinghuan(Integer id){
        return binghuanService.deleteBinghuan(id);
    }
}
